module controller.article_ctrl;

import std.conv : to;
import std.array : appender;

import vibe.http.server : HTTPServerRequest, HTTPServerResponse;

import model.article;
import model.article;
import model.category;
import service.article_service;
import service.cate_service;

import darktemple.render;

/** 
 * 文章详情页
 */
void getArticleDetailPage (scope HTTPServerRequest req, scope HTTPServerResponse res)
{
    string id = req.params.get("id", "");
    if (id == "")
    {
        return res.writeBody("错误的请求", 400);
    }
    int articleId = to!int(id);
    auto art = getArticleById(articleId);
    if (art.isNull)
    {
        return res.writeBody("文章不存在", 404);
    }

    Category[] categoryList = getCategoryList();

    auto article = art.get;
    string html = renderFile!("detail.tmpl", article, categoryList);
    res.writeBody(html, "text/html; charset=utf-8");
}

/** 
 * 文章归档页
 */
void getArticleArchivePage (scope HTTPServerRequest req, scope HTTPServerResponse res)
{
    Category[] categoryList = getCategoryList();
    ArticleArchive[] archiveList = getArticleArchiveList();

    string html = renderFile!("archive.tmpl", categoryList, archiveList);
    res.writeBody(html, "text/html; charset=utf-8");
}
